import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Children from '../components/Children.vue'
import Groupset from '../components/Groupset.vue'
import Recognition from '../components/Recognition.vue'
import Myschedule from '../components/Myschedule.vue'
import DepInSet from '../components/DepInSet.vue'
import Role from '../components/Role.vue'
import Announcement from '../components/Announcement.vue'
import Schdule from '../components/Schdule.vue'
import Staffmanagement from '../components/Staffmanagement.vue'
import Jobtimereport from '../components/Report/Jobtimereport.vue'
import Addreport from '../components/Report/Addreport.vue'
import Showreport from '../components/Report/Showreport.vue'
import OvertimeAbsent from '../components/OvertimeAbsent.vue'
import SharedHardDisk from '../components/SharedHardDisk'
import PublicDisk from '../components/HardDisk/PublicDisk.vue'
import AnotherDisk from '../components/HardDisk/AnotherDisk.vue'
import Empty from '../components/HardDisk/empty.vue'
import Vacate from '../components/Vacate.vue'
import OverTime from '../components/OverTime.vue'
import Meetingroom from '../components/Meeting/Meetingroom.vue'
import Meetingarrange from '../components/Meeting/Meetingarrange.vue'
import MyMeeting from '../components/Meeting/MyMeeting.vue'
import MenuSet from '../components/SystemSet/MenuSet.vue'
import SetChildPage from '../components/SystemSet/setChildPage.vue'
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/children',
    children: [
      {
        path: '/children',
        component: Children
      },
      {
        path: '/groupset',
        component: Groupset,
      },

      {
        path: '/recognition',
        component: Recognition
      },
      {
        path: '/myschedule',
        component: Myschedule
      },
      {
        path: '/vacate',
        component: Vacate
      },
      {
        path: '/depInSet',
        component: DepInSet
      },
      {
        path: '/role',
        component: Role
      },
      {
        path: '/announcement',
        component: Announcement
      },
      {
        path: '/Schdule',
        component: Schdule
      },
      {
        path: '/OvertimeAbsent',
        component: OvertimeAbsent
      },
      {
        path: '/Staffmanagement',
        component: Staffmanagement
      },
      {
        path: '/Jobtimereport',
        component: Jobtimereport,
        redirect: '/Addreport',
        children: [{
          path: '/Addreport',
          component: Addreport
        },
        {
          path: '/Showreport',
          component: Showreport
        }
        ]
      },
      {
        path: '/SharedHardDisk',
        component: SharedHardDisk,
        children: [{
          path: '/PublicDisk',
          component: PublicDisk
        },
        {
          path: '/AnotherDisk',
          component: AnotherDisk
        },
        {
          path: '/Empty',
          component: Empty
        }]
      },
      {
        path: '/OverTime',
        component: OverTime
      },
      {
        path: '/meetingroom',
        component: Meetingroom

      },
      {
        path: '/Meetingarrange',
        component: Meetingarrange
      },
      {
        path: '/MyMeeting',
        component: MyMeeting
      },
      {
        path: '/MenuSet',
        component: MenuSet
      },
      {
        path: '/MenuSetChild',
        component: SetChildPage
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  console.log(to.path)
  if (to.path === '/login') return next()
  let flg = 0

  let path = to.path

  if (path == '/Addreport') {
    path = '/Jobtimereport'
  } else if (path == '/Showreport') {
    path = '/Jobtimereport'
  } else if (path == '/PublicDisk') {
    path = '/SharedHardDisk'
  } else if (path == '/Empty') {
    path = '/SharedHardDisk'
  } else if (path == '/AnotherDisk') {
    path = '/SharedHardDisk'
  } else if (path == '/MenuSetChild') {
    path = '/MenuSet'
  }
  console.log(typeof window.sessionStorage.getItem('vuex'))
  if (path !== '/children') {
    if (window.sessionStorage.getItem('vuex') !== null) {
      JSON.parse(window.sessionStorage.getItem('vuex')).routerInfo.forEach(ele => {
        ele.children.forEach(item => {
          if (path === item.path) {
            flg = 1
          }
        })
      })
    }
  } else {
    flg = 1
  }

  if (window.sessionStorage.getItem('vuex') !== null && flg === 0) {
    window.localStorage.clear()
  }
  const tokenStr = window.localStorage.getItem('token')
  // if (!tokenStr)
  if (tokenStr == null || flg === 0) {
    return next('/login')
  }

  if(tokenStr != null && path == '/children' && window.sessionStorage.getItem('vuex') == null){
    return next('/login')
  }
  if (to.path == '/SharedHardDisk') {
    return next('/PublicDisk')
  }

  next()
})

export default router
